const path = require('path');
const resolve = require('./utils/resolve');
const plugins = require('./utils/plugins');
const { SRC_PATH, DIST_PATH, EXCLUDE_PATH, IS_DEV, IS_PRO } = require('./utils/variable');

const configure = {
    entry:{
        index: path.join(SRC_PATH, 'index.tsx'),
    },
    output: {
        path: DIST_PATH,
        filename: IS_DEV ? 'js/[name].bundle.js' : 'js/[name].[contenthash:8].bundle.js',
        globalObject: 'this',
        chunkFilename: IS_DEV ? 'js/[name].chunk.js' : 'js/[name].[contenthash:8].chunk.js',
        assetModuleFilename: 'assets/[hash][ext][query]',
        clean: true,
    },
    //loader的执行顺序默认从右到左，多个loader用[],字符串只用一个loader，也可以是对象的格式
    module: {
        rules: [
            {
                test: /\.(tsx?|js)$/,
                include: [SRC_PATH],
                use: [
                    {
                        loader: "babel-loader"
                    },
                    /*{
                        loader: 'thread-loader',
                        options: {
                            workers: require('os').cpus().length * 2,
                            parallel: true,
                        },
                    },*/
                    {
                        loader: "ts-loader"
                    },
                ],
                exclude: [/node_modules/, /public/, /(.|_)min\.js$/],
            },
            {
                test: /\.css$|\.less$/i,
                include: [SRC_PATH],
                // 取消匹配node_modules里面的文件
                exclude: EXCLUDE_PATH,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            modules: false,
                            sourceMap: !IS_PRO
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            lessOptions: {
                                modifyVars: {
                                    'root-entry-name': 'variable'
                                }
                            }
                        }
                    },
                    {
                        loader: 'style-resources-loader',
                        options: {
                            patterns: path.resolve(SRC_PATH, 'assets', 'css', 'core.scss'),
                        }
                    },
                ],
            },
        ]
    },
    resolve: resolve,
    plugins: plugins.getPlugins(),
};

module.exports = configure;